package com.rayc.mall.core.config;

import com.rayc.mall.core.interceptor.AdminInterceptor;
import com.rayc.mall.core.interceptor.LoginInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: Ray-C
 * @CreateTime: 2025-03-23
 * @Description: MVC配置
 * @Version: 1.0
 */
@Configuration
@RequiredArgsConstructor
@ComponentScan("com.rayc.mall.core")
public class MvcConfig implements WebMvcConfigurer {
    private final LoginInterceptor loginInterceptor;
    private final AdminInterceptor adminInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/users/login", "/users/register")
                .excludePathPatterns("/error", "/favicon.ico", "/v2/**", "/v3/**", "/swagger-resources/**", "/webjars/**", "/doc.html");
        registry.addInterceptor(adminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/error", "/favicon.ico", "/v2/**", "/v3/**", "/swagger-resources/**", "/webjars/**", "/doc.html");
    }

    // 网关依旧设置跨域配置，这里再设置会导致有多个Access-Control-Allow-Origin头，前端跨域报错
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        // 所有的当前站点的请求地址，都支持跨域访问。
//        registry.addMapping("/**")
//                .allowedOrigins("http://localhost:5000", "http://127.0.0.1:5000")
//                .allowCredentials(true)
//                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE", "OPTIONS"});
//    }
}
